<?php include_once("../db_config.php");
session_start();
	# 查询商品名字
	function item_name($id,$type){

		if ($id != NULL) {
			$sql = mysql_query("SELECT * FROM item_template_tables WHERE entry = $id");

			if (!$sql) {
				 echo "查询错误";
			}
			else{
				while ($row = mysql_fetch_assoc($sql)) {
					return $row["item_name"];
				}
			}

		}

		if ($type != NULL) {
			$sql = mysql_query("SELECT * FROM item_template_tables WHERE type = $type");

			if (!$sql) {
				echo "查询错误";
			}
			else{
				while ($row = mysql_fetch_assoc($sql)) {
					echo "<td>" . $row["item_name"] . "</td>";
				}
			}
		}


	}
	#查询商品分类
	function item_type($id){
		if ($id != NULL) {
			$sql = mysql_query("SELECT type FROM item_template_tables WHERE entry = $id");

			if (!$sql) {
				echo "查询错误";
			}
			else{
				while ($row = mysql_fetch_assoc($sql)) {
					return $row["type"];
				}
			}
		}
	}

	function inventory_query(){
		$sql = mysql_query("SELECT * FROM inventory_tables");
			while ($row = mysql_fetch_assoc($sql)) {
				$id = $row["entry"];
				$t_num = $row['t_num'];
				$m_num = $row["min_num"];
				$b_num = $row["bailu_num"];
				$sun = $t_num + $m_num + $b_num;
				echo "<tr>";
				echo "<td>" . $id . "</td>";
				echo "<td>" . item_name($id) . "</td>";
				echo "<td>" . item_type($id) . "</td>";
				echo "<td>" . $t_num . "</td>";
				echo "<td>" . $m_num . "</td>";
				echo "<td>" . $b_num . "</td>";
				echo "<td>" . $sun . "</td>";
				echo "</tr>";
			}
		
	}

	// 查询会员编号
	function vip_account_query($value){
		$query = mysql_query("SELECT `entry`,`acc` FROM xrk_vip WHERE acc = '$value'");
		if (!$query) {
			echo "查询失败";
		}
		else if(mysql_num_rows($query) > 0){
			while ($row = mysql_fetch_assoc($query)) {
				return $row["entry"];
			}
		}
		else{
			echo "没有这个会员";
		}
	}

	// 查询会员剩余免费次数
	function vip_free_num($value)
	{
		$query = mysql_query("SELECT `free_num` FROM xrk_vip WHERE acc = '$value'");
		if (!$query) {
			echo "查询失败";
		}
		else if(mysql_num_rows($query) > 0){
			while ($row = mysql_fetch_assoc($query)) {
				return $row["free_num"];
			}
		}
		else{
			echo "查询失败";
		}
	}
	// 查询会员积分
	function vip_rewards_query($acc = NULL,$vid){

		if ($vid != NULL) {
			$sql = mysql_query("SELECT SUM(rewards_add) `rewards_add`,SUM(rewards_del) `rewards_del` FROM xrk_vip_rewards WHERE e_id = $vid");
		}
		elseif ($acc != NULL) {
			$id = vip_account_query($acc);
			$sql = mysql_query("SELECT SUM(rewards_add) `rewards_add`,SUM(rewards_del) `rewards_del` FROM xrk_vip_rewards WHERE e_id = $id");
		}

		if (!$sql) {
			echo "查询失败";
		}
		else if(mysql_num_rows($sql) > 0){
			while ($row = mysql_fetch_assoc($sql)) {
					$rewards_all = $row['rewards_add'];
					$rewards_del = $row['rewards_del'];
					$rewards_num = $rewards_all - $rewards_del;
					echo "当前积分为：".$rewards_num."，免费上门次数为：";
					echo $n = vip_free_num($acc) == 0 ? "0" : vip_free_num($acc);

			}
		}
		else{
			echo "没有这个会员";
		}
	}

	if(isset($_POST["start"])){
		 $start = addslashes($_POST["start"]); # 获取类别

		 switch ($start) {
		 	case 'query_vip_var':
		 		$call = addslashes($_POST["call_main"]);
		 		vip_rewards_query($call);
		 		break;
		 	
		 	default:
		 		# code...
		 		break;
		 }
	}
?>